package com.rdExpenses.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.common.persistence.Page;
import com.base.common.utils.DateUtils;
import com.company.model.CompanyProject;
import com.company.service.ProjectManageService;
import com.rdExpenses.model.ExpenseAssign;
import com.rdExpenses.model.ExpenseAssignItem;
import com.rdExpenses.model.ExpenseInfo;
import com.rdExpenses.model.ExpenseType;
import com.rdExpenses.service.ExpenseAssignItemService;
import com.rdExpenses.service.ExpenseAssignService;
import com.rdExpenses.service.ExpenseService;
import com.rdExpenses.service.ExpenseTypeService;
import com.system.model.UserInfo;
import com.system.service.UserInfoService;

@Controller
public class ExpenseController {
	// 页面actionView的地址
	public static final String toExpenseView = "/expense";
	public static final String toExpenseListView = "/expenseList";
	public static final String expenseList = "/projectExpense/list";
	// public static final String expenseEdit = "/projectExpense/edit";
	public static final String expenseSave = "/projectExpense/save";
	public static final String expenseDelete = "/projectExpense/delete";
	public static final String expenseAssignCommit = "/projectExpense/commit";

	// 页面路径
	public static final String expensePage = "/rdExpenses/projectExpense";
	public static final String expenseListPage = "/rdExpenses/projectExpenseList";

	@Autowired
	ExpenseService expenseService;
	@Autowired
	ProjectManageService projectManageService;
	@Autowired
	ExpenseTypeService expenseTypeService;
	@Autowired
	ExpenseAssignService expenseAssignService;
	@Autowired
	UserInfoService userInfoService;
	@Autowired
	ExpenseAssignItemService expenseAssignItemService;

	@RequestMapping(value = toExpenseView, method = RequestMethod.POST)
	public ModelAndView toExpenseView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(expensePage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<CompanyProject> list = null;
		try {
			list = projectManageService.findProjectByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		mv.addObject("projectSelect", list);

		List<ExpenseType> ExpenseTypeList = null;
		try {
			ExpenseTypeList = expenseTypeService.findExpenseTypeList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("expenseTypeSelect", ExpenseTypeList);
		return mv;
	}

	@RequestMapping(value = toExpenseListView, method = RequestMethod.POST)
	public ModelAndView toExpenseListView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(expenseListPage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<ExpenseInfo> list = null;
		try {
			list = expenseService.findExpenseListByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("expenseList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = expenseAssignCommit, method = RequestMethod.POST)
	public boolean expenseAssignCommit(String assignId) {
		boolean result = false;
		try {
			ExpenseAssign expenseAssign = expenseAssignService.findExpenseAssignById(assignId);
			expenseAssign.setStatus("1");
			expenseAssignService.saveExpenseAssign(expenseAssign);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = expenseSave, method = RequestMethod.POST)
	public boolean expenseSave(HttpServletRequest req, HttpServletResponse res, ExpenseInfo expenseInfo) {
		String companyId = req.getSession().getAttribute("companyId").toString();
		boolean result = false;
		try {
			if (expenseInfo.getProjectId().equals("-1")) {
				ExpenseType expenseType = expenseTypeService.findExpenseTypeById(expenseInfo.getExpenseTypeId());
				ExpenseAssign expenseAssign = new ExpenseAssign();
				expenseAssign.setTitle("费用分配");
				expenseAssign.setContent(expenseType.getExpenseName() + "项目分配");
				expenseAssign.setStatus("0");
				expenseAssign.setExpense(expenseInfo.getExpense());
				UserInfo userInfo = userInfoService.getUserInfoByCompanyIdAndType(companyId, "P");
				expenseAssign.setUserId(userInfo.getId());
				result = expenseAssignService.saveExpenseAssign(expenseAssign);
				return result;
			}
			expenseInfo.setCompanyId(companyId);
			expenseInfo.setBillingDate(DateUtils.formatDateTime(new Date()));
			result = expenseService.saveExpense(expenseInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = expenseDelete, method = RequestMethod.POST)
	public boolean expenseDelete(String ids) {
		boolean result = false;
		try {
			result = expenseService.deleteExpenseByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
